package com.cn.dim.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Dep;
import com.cn.dim.pojo.Pages;
import com.cn.dim.pojo.Position;
import com.cn.dim.service.DepService;
import com.cn.dim.utils.TreeBuilder;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/dep")
public class DepController {

	@Resource
	private DepService depService;

	@RequestMapping(value ="/queryDep",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryPostion(HttpServletRequest request,Position p,Pages pages){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		param.put("pageIndex",  pages.getPage());
		param.put("pageSize",pages.getRows());

		try {
			Page<Map<String,Object>> rList = (Page)depService.queryDep(param);
			for(Map m:rList){
				if(m.get("pid").equals("0")){
					//	list.add(m);
					//getClild(m,rList);

				}
			}
			System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryDepartment",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryDepartment(HttpServletRequest request,Dep p,Pages pages){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("depnum", p.getDepnum());
		param.put("depname", p.getDepname());
		param.put("pageIndex", pages.getPage());
		param.put("pageSize", pages.getRows());

		try {
			Page<Map<String,Object>> rList = (Page)depService.queryDep(param);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryDepartmentList",method = RequestMethod.GET)
	public String queryDepartmentList(HttpServletResponse response){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		List<Dep> rList = new ArrayList<>();
		Map<String,Object> params = new  HashMap<>();
		try
		{
			rList =depService.queryDepartmentList(params);
			// 获取全部目录节点
	        List<Dep> nodes = rList;
	        // 拼装树形json字符串
	        String json = new TreeBuilder().buildTree(nodes);
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(json);
			response.getWriter().flush();
		}
		catch (IOException e){
			e.getStackTrace();
		}
		return null;
	}
	@RequestMapping(value ="/insertDep",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> insertDep(Dep d,String depzwlist){
		Map<String,Object> reMap=new HashMap<>();
		Map<String,Object> params=new HashMap<>();
		String uuid = com.cn.dim.utils.StringUtil.makeUUID();
		d.setId(uuid);
		d.setIsdel("0");
		JSONArray jsonArray=JSONArray.fromObject(depzwlist);
        List<Map<String,Object>> list=(List<Map<String,Object>>)JSONArray.toList(jsonArray, Map.class);
        for(Map m:list){
        	m.put("depid", uuid);//构造插入中间表的参数
        }
		
		//这里补充创建人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//创建日期
			d.setCreatetime(cdate);
			
			int flag = depService.insertDep(d,list);
			if(flag>0){
				reMap.put("message", "插入成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryDepByLevel1",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryDepByLevel1(HttpServletRequest request){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("pageIndex", 1);
		param.put("pageSize",10);

		try {
			List<Map<String,Object>> rList = depService.queryDepByLevel1(param);

			reMap.put("rows", rList);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryDepBeanById",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryDepBeanById(HttpServletRequest request,Position p){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", p.getId());
		try {
			Dep pos =depService.queryDepBeanById(param);
			reMap.put("data", pos);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/queryDepAndPosition",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> queryDepAndPosition(HttpServletRequest request,Position p){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", p.getId());
		try {
			List<Map<String,Object>> list =depService.queryDepAndPosition(param);
			reMap.put("data", list);
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/checkChild",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> checkChild(HttpServletRequest request,Position p){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", p.getId());
		try {
			List<Map<String,Object>> list =depService.checkChild(param);
			if(list.size()>0){
				reMap.put("data", "yes");
				reMap.put("message", "该部门下存在子部门删除会联并删除");
			}else{
				reMap.put("data", "no");
			}
			
			reMap.put("status", "success");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/delDep",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delDep(HttpServletRequest request,Position p){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("depid", p.getId());
		
		//这里补充修改（删除）人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//修改（删除）日期
			param.put("updatetime", cdate);
			
			
			int flag =depService.delDep(param);
			if(flag>0){
				reMap.put("message", "删除成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
	@RequestMapping(value ="/updateDep",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> updateDep(HttpServletRequest request,Dep d,String depzwlist){
		Map<String,Object> param=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		param.put("id", d.getId());
		JSONArray jsonArray=JSONArray.fromObject(depzwlist);
        List<Map<String,Object>> list=(List<Map<String,Object>>)JSONArray.toList(jsonArray, Map.class);
        for(Map m:list){
        	m.put("depid", d.getId());
        }
		
		//这里补充修改人的id的代码
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date cdate = sdf.parse(sdf.format(new Date()));//修改日期
			d.setUpdatetime(cdate);
			
			int flag =depService.updateDep(d,list);
			if(flag>0){
				reMap.put("message", "修改成功！");
				reMap.put("status", "success");
			}else{
				reMap.put("status", "error");
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
		}
		return reMap;
	}
}
